#include "database.h"

Database::Database() : _state(false), _res(nullptr), _column(nullptr) {
	cout << "create database...." << endl;
	_conn = new MYSQL();
}
Database::~Database() {
	cout << "disconncect...." << endl;
}
bool Database::connect(string const ip, string const name, string const pass, string const dbname, int const port) {
	if (_state)
		return false;

	mysql_init(_conn);
	if (mysql_real_connect(_conn, ip.c_str(), name.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0)) {
		_state = true;
		mysql_set_character_set(_conn, "gbk");
		return true;
	}
}
int Database::getTableField(string const tablename) {
	if (!_state)
		return -1;
	string sql = "show columns from " + tablename;

	if (mysql_query(_conn, sql.c_str()))
		return -1;

	_res = mysql_store_result(_conn);
	if (_res == nullptr)
		return -1;
	return mysql_affected_rows(_conn);
}

string Database::queryall(string const tablename) {
	if (!_state)
		return "";

	int row = getTableField(tablename);

	string sql = "select * from " + tablename;

	if (mysql_query(_conn, sql.c_str()))
		return "";

	_res = mysql_store_result(_conn);
	if (_res == nullptr)
		return "";

	fd.reserve(row);
	fd.resize(row);

	for (int i = 0; i < row; i++)
		fd[i] = mysql_fetch_field(_res);

	string res = "";
	while (_column = mysql_fetch_row(_res)) {
		for (int i = 0; i < row; i++)
			res += _column[i], res += "\t";
		res += "\n";
	}
	return res;
}


bool Database::queryByNameAndPass(string name, string pass, string tablename) {
	if (!_state)
		return false;

	string sql = "select * from "+ tablename +" where username = '" + name + "' and password='" + pass + "';";

	/*cout << sql;*/
	if (mysql_query(_conn, sql.c_str()))
		return false;
	else {
		_res = mysql_store_result(_conn);
		if (_column = mysql_fetch_row(_res)) {
			return true;
		}
		else {
			return false;
		}
	}
}

string Database::searchBookByName(string name) {
	if (!_state)
		return "";

	int row = getTableField("book");

	string sql = "select * from book where name='" + name+"'";

	if (mysql_query(_conn, sql.c_str()))
		return "";

	_res = mysql_store_result(_conn);
	if (_res == nullptr)
		return "";

	
	string res = "";
	while (_column = mysql_fetch_row(_res)) {
		for (int i = 0; i < row; i++)
			res += _column[i], res += "\t";
		res += "\n";
	}
	return res;
}

bool Database::updateBookByName(vector<string> tokens) {
	if (!_state)
		return false;

	string sql = "update book set author='" + tokens[1] + "', publisher='" + tokens[2] + "',";
	sql = sql + "isbn='" + tokens[3] + "', price=" + tokens[4] + ", pubdate='" + tokens[5] + "' where name='" + tokens[0] + "';";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}
		
}

bool Database::addBookInfo(vector<string> tokens) {
	if (!_state)
		return false;

	string sql = "insert into book (name, author, publisher,isbn, price, pubdate) values ('";
	sql = sql+ tokens[0] + "','"+tokens[1] + "','" + tokens[2] + "','" + tokens[3] + "'," + tokens[4] + ", '" + tokens[5] + "');";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}
}

bool Database::deleteBookByName(string name) {
	if (!_state)
		return false;

	string sql = "delete from book where name ='" + name + "';";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}
}


string Database::searchUserByUName(string uname, string tablename) {
	if (!_state)
		return "";

	int row = getTableField(tablename);

	string sql = "select * from "+tablename+" where username='" + uname + "'";

	if (mysql_query(_conn, sql.c_str()))
		return "";

	_res = mysql_store_result(_conn);
	if (_res == nullptr)
		return "";


	string res = "";
	while (_column = mysql_fetch_row(_res)) {
		for (int i = 0; i < row; i++)
			res += _column[i], res += "\t";
		res += "\n";
	}
	return res;
}

bool Database::updateUserPass(string uname, string pass, string tablename) {
	if (!_state)
		return false;

	string sql = "update "+tablename+" set password='" + pass + "' where username='" + uname + "';";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}
}

bool Database::updateUser(string uname, vector<string> tokens, string tablename) {
	if (!_state)
		return false;

	string sql = "update "+tablename+" set name='" + tokens[0] + "', major='" + tokens[1] + "',";
	sql = sql + "age=" + tokens[2] + ", sex='" + tokens[3] + "' where username='" + uname + "';";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}

}

bool Database::insertUser(string uname, vector<string> tokens, string tablename) {
	if (!_state)
		return false;

	string sql = "insert into "+tablename+" (username, name, major,age, sex) values ('";
	sql = sql + uname + "','" + tokens[0] + "','" + tokens[1] + "'," + tokens[2] + ",'" + tokens[3] + "');";

	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}

	sql = "insert into user (username, password) values ('" + uname + "','123');";
	cout << sql;

	if (mysql_query(_conn, sql.c_str())) {
		return false;
	}
	else {
		return true;
	}
}